package com.bcx.crm.java.proxy.jdkProxy1;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created by ys on 2018/1/22.
 */
public class AppServiceTest implements InvocationHandler {
    private Object target; //目标对象的引用
    public AppServiceTest(Object target){
        this.target=target;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("Entered:"+target.getClass().getName()+"-- method:"+method.getName()+"--args:"+args[0]);
        Object result=method.invoke(target,args);
        System.out.println("return before "+result);
        return result;
    }
    public static void main(String [] strings){
        AppServiceImpl target=new AppServiceImpl();//生成目标对象
        //创建代理对象
        IAppService iAppService=(IAppService) Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        Object result = method.invoke(target,args); //调用目标对象的方法
                        return result;
                    }
                });
        iAppService.createApp("2018-1-23");
    }
}
